package com.cskaoyan.converter;

import lombok.SneakyThrows;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class String2DateConverter implements Converter<String, Date> {
    @SneakyThrows
    @Override
    public Date convert(String param) {
        //自定义的转换业务
        if (null != param && param.length() == 10) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date date = simpleDateFormat.parse(param);
            return date;
        }
        if (null != param && param.length() == 19) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = simpleDateFormat.parse(param);
            return date;
        }
        //更灵活、更强壮
        if (param != null && (param.startsWith("20") || param.startsWith("19"))) {

        }

        return null;
    }
}
